package com.mucd.chat.service.support;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.websocket.Session;

/**
 * @author mucd
 */
@Service
@Slf4j
public class ChatSocketSupport {

    public void checkMessage(String message) {
        if (StrUtil.isEmpty(message)) {
            log.error("消息内容不能为空");
            throw new ServiceException("消息内容不能为空");
        } else {
            if (message.length() > 1000) {
                log.error("消息内容不能超过1000个字符");
                throw new ServiceException("消息内容不能超过1000个字符");
            }
        }
    }

    public boolean checkOnline(Session targetSession) {

        boolean open = targetSession.isOpen();
        if (!open) {
            log.info("用户已离线");
            return false;
        }

        return true;

    }
}
